package com.cnsuning.jobtool.jobtools.transfer;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 〈一句话功能简述〉
 *
 * @author 88446845
 * @date 2019/12/23
 */
public class C3P0Mysqls {


    public static C3P0Mysqls c3P0Properties;
    public static String USERNAME="omsduser";
    private static Map<String,ComboPooledDataSource> dataSource = new HashMap<>(20);
    static{
        c3P0Properties = new C3P0Mysqls();
    }

    public C3P0Mysqls() {
        try {
            //加载配置文件
            Properties props = new Properties();
            props.load(new BufferedReader(new FileReader("D:\\idea_code\\boot\\src\\main\\java\\com\\spring\\boot\\boot\\qianyi\\db.properties")));
            creatDataSource((String) props.get("db2driver"),(String) props.get("pre.omsddb2sever31.url"),(String) props.get("pre.omsddb2sever31.userName"),(String) props.get("pre.omsddb2sever31.password"),USERNAME);
            creatDataSource((String) props.get("db2driver"),(String) props.get("pre.omsddb2sever32.url"),(String) props.get("pre.omsddb2sever32.userName"),(String) props.get("pre.omsddb2sever32.password"),USERNAME);
            creatDataSource((String) props.get("db2driver"),(String) props.get("pre.omsddb2sever33.url"),(String) props.get("pre.omsddb2sever33.userName"),(String) props.get("pre.omsddb2sever33.password"),USERNAME);
            creatDataSource((String) props.get("db2driver"),(String) props.get("pre.omsddb2sever34.url"),(String) props.get("pre.omsddb2sever34.userName"),(String) props.get("pre.omsddb2sever34.password"),USERNAME);
            creatDataSource((String) props.get("db2driver"),(String) props.get("pre.omsddb2sever35.url"),(String) props.get("pre.omsddb2sever35.userName"),(String) props.get("pre.omsddb2sever35.password"),USERNAME);
            creatDataSource((String) props.get("db2driver"),(String) props.get("pre.omsddb2sever36.url"),(String) props.get("pre.omsddb2sever36.userName"),(String) props.get("pre.omsddb2sever36.password"),USERNAME);
            creatDataSource((String) props.get("db2driver"),(String) props.get("pre.omsddb2sever37.url"),(String) props.get("pre.omsddb2sever37.userName"),(String) props.get("pre.omsddb2sever37.password"),USERNAME);
            creatDataSource((String) props.get("db2driver"),(String) props.get("pre.omsddb2sever38.url"),(String) props.get("pre.omsddb2sever38.userName"),(String) props.get("pre.omsddb2sever38.password"),USERNAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void creatDataSource(String driver,String url,String schema,String password,String username){
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        try {
            cpds.setDriverClass(driver);
            cpds.setJdbcUrl(url);
            cpds.setUser(username);
            cpds.setPassword(password);
            cpds.setMaxPoolSize(10);
            cpds.setMinPoolSize(1);
            cpds.setInitialPoolSize(1);
            cpds.setMaxStatements(999999999);
            cpds.setMaxIdleTime(999999999);
            dataSource.put(schema.toUpperCase(),cpds);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
    public static C3P0Mysqls getInstance(){
        return c3P0Properties;
    }

    public  Connection getConnection(String scheam){
        Connection conn = null;
        try {
            conn = dataSource.get(scheam).getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void main(String[] args) {
        Connection connection = C3P0Mysqls.c3P0Properties.getConnection("01");

        System.out.println("已经连接成功");

    }

}
